package com.eclipsemail.ui.view;

import java.util.ArrayList;

import net.eclipsemail.core.authentication.impl.GMailAuthenticationFake;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.List;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.ViewPart;

import com.eclipsemail.core.authentication.AuthenticationException;
import com.eclipsemail.core.message.EmailMessage;
import com.eclipsemail.core.reader.impl.GMailReader;
import com.eclipsemail.ui.editor.MailEditorInput;
import com.eclipsemail.ui.editor.MailEditorPart;

public class ListMailControl {

    private final ViewPart view;
    private final Composite parent;
    private int style = SWT.NONE;

    private static final String SPACES = "    ";
    private List list;
    private java.util.List<EmailMessage> listMails;

    public ListMailControl(ViewPart view, Composite parent) {
        this.view = view;
        this.parent = parent;
    }

    public void init() {
        createList();
        addItens();
        createDoubleClickListener();
    }

    private void createList() {
        list = new List(parent, style);
        listMails = new ArrayList<EmailMessage>();
    }

    private void addItens() {
        try {
            java.util.List<EmailMessage> inbox = new GMailReader().getInbox(new GMailAuthenticationFake());
            listMails.addAll(inbox);
        } catch (AuthenticationException e) {
            throw new RuntimeException(e);
        }

        for (EmailMessage mail : listMails) {
            list.add(SPACES + mail.getTitle());
        }
    }

    private void createDoubleClickListener() {
        list.addMouseListener(new MouseEventLister());
    }

    private class MouseEventLister implements MouseListener {

        @Override
        public void mouseDoubleClick(MouseEvent event) {
            if (event.button == 1) { // 1 = left click
                showEditor();
            }
        }

        @Override
        public void mouseDown(MouseEvent arg0) {
            // nop
        }

        @Override
        public void mouseUp(MouseEvent arg0) {
            // nop
        }

    }

    private void showEditor() {
        int index = list.getSelectionIndex();
        EmailMessage mail = listMails.get(index);
        MailEditorInput editorInput = new MailEditorInput(mail);

        try {
            IWorkbenchPage page = view.getSite().getPage();
            IEditorPart editor = page.findEditor(editorInput);
            if (editor == null) {
                page.openEditor(editorInput, MailEditorPart.ID, true);
            } else {
                page.activate(editor);
            }
        } catch (PartInitException e) {
            throw new RuntimeException(e);
        }

    }

}
